// JavaScript Document
var webtitle;
$(function(){
	//检查是否登录
	if ($.cookie("hlhtName") == null || $.cookie("hlhtName") == '') {
		location.href=configParam.htmlPage.backGround + 'login.html';
	}
	$('#loginAccount').html($.cookie("hlhtName"));
	getOtherProCat();
	getmenu();//获取菜单
	getParam();
	
});

//获取菜单
function getmenu(){
	var fn="getmenu";
	var params = Commonjs.getParams(fn,"");//获取参数
	var data=Commonjs.ajax(sysurl,params,false);
	var funCodeList = {};
	var topMenum="";
	if(data.result=="success"){
        var menllist="";
        BaseForeach(data.data,function(i,item) {
            if(item.parentId=="1"){
            	topMenum+="<li funId=\"" + item.funId + "\"><a href=\"javascript:void (0)\">" + item.title + "</a></li>";

                menllist+=' <li class="nav-header" pFunId="' + item.funId + '" style="display: none">';
                menllist+=' <div class="dropdown profile-element center" style="width:120px; margin-left:-30px;">';
                //menllist+=' <i class="iconfont ' + item.resource + '" style="font-size: 45px"></i>';
                //menllist+=' <span class="clear">';
                menllist+=' <span class="block m-t-xs center" id="acount_name" style="font-size: 22px">'+item.caption+'</span>';
                menllist+=' </span>';
                menllist+=' </a>';
                menllist+=' </div>';
                menllist+=' </li>';

            }
        });

        BaseForeach(data.data,function(k,prtitem){
            if(prtitem.parentId=="2" && prtitem.isMenu==1){
            	if(prtitem.funId == '65') {
                    /*menllist += '<li pFunId="' + prtitem.funId.substring(0, 1) + '" style="display: none">
                    <a href="javascript:void(0);" data-url="' + prtitem.moduleUrl + '" class="otherServerBtn" >' +
                        '<i class="iconfont ' + prtitem.resource + '"></i>' + ' ' +
                        ' <span >' + prtitem.title + '</span>' +
                        '<i class="iconfont icon-arrow-down-copy" style="display: inline-block;position: relative;float: right;"></i>' +
                        '</a>';
                    menllist += '<ul class="sub-menu" style="display:block;">';*/
                    BaseForeach(vmGaea.otherServerList, function (key, item) {
                        if (item.adminDiyPage != '' && item.adminDiyPage != undefined && item.adminDiyPage != null) {
                            var service = {serviceCode: item.serviceCode};
                            var token = "";
                            var fn = "mimiServicePublicManageToken";
                            service = Commonjs.jsonToString(service);
                            var params = Commonjs.getParams(fn, service);
                            Commonjs.ajaxSilence(weburl, params, false, function (data) {
                                token = data.data.token;
                                menllist += '<li pFunId="' + prtitem.funId.substring(0, 1) + '" style="display: none">' +
                                    '<a href="' + item.adminDiyPage + ' class="J_menuItem">' +
									'<i class="iconfont ' + prtitem.resource + '"></i>' +
									'<span class="nav-label">' + item.categoryName + '</span></a></li>';
                            }, function (data) {
                            });
                        } else {
                            menllist += '<li pFunId="' + prtitem.funId.substring(0, 1) + '" style="display: none">' +
                                '<a href="' + prtitem.moduleUrl  +'?subClass='+item.categoryCode+ '" class="J_menuItem">' +
								'<i class="iconfont ' + prtitem.resource + '"></i>' +
								'<span class="nav-label">' + item.categoryName + '</span></a></li>'
                        }

                    });
                    /*menllist += '</ul></li>';*/
                } else if (prtitem.funId == '3j') {
                    menllist += '<li pFunId="' + prtitem.funId.substring(0, 1) + '" style="display: none"><a href="javascript:void(0);" data-url="' + prtitem.moduleUrl + '" class="otherServerBtn" >' +
                        '<i class="iconfont ' + prtitem.resource + '"></i>' + ' ' +
                        ' <span >' + prtitem.title + '</span>' +
                        '<i class="iconfont icon-arrow-down-copy" style="display: inline-block;position: relative;float: right;"></i>' +
                        '</a>';
                    menllist += '<ul class="sub-menu" style="display:block;">';
                    BaseForeach(vmGaea.otherServerList, function (key, item) {
                        if (item.adminDiyPage != '' && item.adminDiyPage != undefined && item.adminDiyPage != null) {
                            var service = {serviceCode: item.serviceCode};
                            var token = "";
                            var fn = "mimiServicePublicManageToken";
                            service = Commonjs.jsonToString(service);
                            var params = Commonjs.getParams(fn, service);
                            Commonjs.ajaxSilence(weburl, params, false, function (data) {
                                token = data.data.token;
                                menllist += '<li class="sub-menu-item nav-label">' +
                                    '<a class="J_menuItem sub-item-a" data-flag="true" href="' + item.adminDiyPage + '?token=' + token + '">' + item.categoryName + '</a></li>'
                                // window.open(row.serviceManageUrl + "?token=" + data.data.token);
                            }, function (data) {
                            });
                        } else {
                            menllist += '<li class="sub-menu-item nav-label">' +
                                '<a class="J_menuItem sub-item-a" data-flag="true" href="' + prtitem.moduleUrl + '?subClass=' + item.categoryCode + '">' + item.categoryName + '</a></li>'
                        }

                    });
                    menllist += '</ul></li>';
            	}else{
            		menllist += '<li pFunId="' + prtitem.funId.substring(0,1) + '" style="display: none"><a href="' +
					prtitem.moduleUrl + '" class="J_menuItem">' +
						'<i class="iconfont ' + prtitem.resource + '"></i>' + ' ' +
						' <span class="nav-label">' + prtitem.title + '</span>'+
						'</a>';
					menllist +='</li>';
            	}
        
                $.each(prtitem.funcs, function(index, subItem) {
                    funCodeList[subItem.id] = subItem.title;
                });
            }
        });

		$('#topBar').html(topMenum);
		$('#side-menu').html(menllist);
		var flag = 0;
		$(".otherServerBtn").on("click",function(){
		 	var url = $(this).attr("data-url");
		 	// window.location.href =url;
		 	if(flag == 0){
		 		$(".sub-menu li").css("display","none");
		 		$(".icon-arrow-down-copy").css("transform","rotate(180deg)");
            	$(this).siblings(".sub-menu").children(".sub-menu-item").css("display","block");
		 		flag = 1;
		 	}else{
		 		$(".sub-menu li").css("display","none");
            	$(this).siblings(".sub-menu").children(".sub-menu-item").css("display","none");
		 		$(".icon-arrow-down-copy").css("transform","rotate(0deg)");
		 		// $(".sub-menu-item").removeClass("sub-item-active");
		 		flag = 0;
		 	}
	    });
		$(".sub-item-a").on("click",function(){
			$(".sub-menu-item").removeClass("sub-item-active");
			$(this).parent("li").addClass("sub-item-active");
		});

		//菜单选中
        $('#side-menu').find('a').on('click', function () {
        	$('#side-menu').find('li').removeClass('active');
            $(this).parent().addClass('active');
            var flag = $(this).attr("data-flag");
            if(flag == undefined || flag == null || flag != 'true'){
            	$(".sub-menu-item").removeClass("sub-item-active");
            }
            // $(".sub-menu li").css("display","none");
            // $(this).siblings(".sub-menu").children(".sub-menu-item").css("display","block");
        });

		//导航切换
		$('#topBar').find('li').unbind('click').on('click', function () {
			$('#topBar').find('li').removeClass('selected');
            $(this).addClass('selected');

			var funId = $(this).attr('funId');
			$('#side-menu').find('li').css('display', 'none');
            $('#side-menu').find('li[pFunId="' + funId + '"]').css('display', '');

            $($('#side-menu').find('li[pFunId="' + funId + '"]').get(1)).find('a').trigger('click');
        });
		$($('#topBar').find('li').get(0)).trigger('click');
	}
	window.localStorage.setItem(configParam.session.funCode, Commonjs.jsonToString(funCodeList));
}

function goToMinServer(serviceCode) {
    var self = this;
    var service = {
        serviceCode: row.serviceCode
    };
    var token = "";
    var fn = "mimiServicePublicManageToken";
    service = Commonjs.jsonToString(service);
    var params = Commonjs.getParams(fn,service);
    Commonjs.ajaxSilence(weburl,params,true,function (data) {
    	token = data.data.token;
    	return token;
        // window.open(row.serviceManageUrl + "?token=" + data.data.token);
    }, function (data) {
        self.$message.error(data.msg);
    });
}

function getParam(){
	var service = {};
	service.type = 1;
	var fn="getparam";
	service = Commonjs.jsonToString(service)
	var params = Commonjs.getParams(fn,service);//获取参数
	var data=Commonjs.ajax(sysurl,params,false);
	if(data==null)
		return false;
	if(data.result=="success"){
		BaseForeach(data.data, function (i, item) {
			switch(item.paramName){
				case "webtitle":
					webtitle=item.paramValue;
					break;
			}
		});
	}
}

function logout(){
	var dialog=	art.dialog({
		lock : true,
		opacity : 0.4,
		width : 250,
		title : '提示',
		content : '确定退出？',
		ok : function() {
		var fn="logout";
		var params = Commonjs.getParams(fn,"");//获取参数
		$.ajax({
			datatype: "json",
			type: "POST",
			url: sysurl,
			data: params,
			timeout: 8000,
			cache: false,
			success: function(obj) {
				var data = jQuery.parseJSON(obj);
				if(data.result=="success" || data.code=="S302"){
					$.cookie("hlhtName", null,{expires:-1,path:"/"});
					window.location.href='login.html';
				}
				else
				{
					$.cookie("hlhtName", null,{expires:-1,path:"/"});
				}
			},
			error: function() {
				closeload();
				alertNew('服务器忙，请稍候再试！');
			}
		})
	},
	cancel: function(){
		$('#dialog').hide();
	}
	});
	 $(".aui_close").hide();//隐藏关闭
}

function uptadminpas(){
	var content=$('#addBox').html();
	var dialog=	art.dialog({
		lock : true,
		opacity : 0.5,
		width : 450,
		title : "管理员修改密码",
		content : content,
		ok : function() {
			if(Commonjs.isEmpty($("#oldPassword").val())){
				$.tooltip('原密码不能为空',2000,false);
				$("#oldPassword").focus();
				return false;
			}
			if(Commonjs.isEmpty($("#newPassword").val())){
				$.tooltip('新密码不能为空',2000,false);
				$("#newPassword").focus();
				return false;
			}
			 if ($.trim($("#repeatPassword").val()) == "" || $("#newPassword").val() != $("#repeatPassword").val()) {
			     	$.tooltip('两次密码输入不一致，请重新输入', 2000, false);
			    	$('#repeatPassword').focus();
					return false;
			     }
			 var service = {};
				service.oldPassword = cloudEncrypt.encodeSession($("#oldPassword").val());
				service.newPassword = cloudEncrypt.encodeSession($("#newPassword").val());
				var fn="uptadminpas";
				service = Commonjs.jsonToString(service);
				var params = Commonjs.getParams(fn,service);//获取参数
				$.ajax({
					datatype: "json",
					type: "POST",
					url: weburl,
					data: params,
					timeout: 8000,
					cache: false,
					beforeSend: function() {
						divalertLoad('正在加载数据,请稍等');
					},
					success: function(obj) {
						var data = jQuery.parseJSON(obj);
						if(data.result == "success"){
							$.tooltip(data.msg, 2000, true);
							location.reload();
                            closeload();
						}else{
							$.tooltip(data.msg, 2000, false);
						}
						if (data.result == "failure") {
							if (data.result == "S202") {
								if (url == weburl) {
									$.cookie("hluserName", null, {
										expires: -1,
										path: "/"
									});
									top.location.href = realPath + configParam.htmlPage.backGround + 'login.html';
								} else {
									$.cookie("hlhtName", null, {
										expires: -1,
										path: "/"
									});
									Commonjs.alerturl(data.msg, realPath + configParam.htmlPage.backGround + 'login.html');
								}
								return false;
							} else {
								$.tooltip(data.msg, 2000, false);
								return false;
							}
						}
					},
					error: function() {
						closeload();
						alertNew('服务器忙，请稍候再试！');
					}
				});
		},
		cancel: function(){
			$('#dialog').hide();
		}
	});
}

function getOtherProCat(){
	var self = this;
	var fn="allDiyProCat";
	var params = Commonjs.getParams(fn,"");//获取参数
	var data=Commonjs.ajax(weburl,params,false);
	vmGaea.otherServerList = data == undefined?[]:data.data;
}

function _topError(message) {
    $.tooltip(message,2000,false);
}

function _topSuccess(message) {
    $.tooltip(message,2000,true);
}


var vmGaea = new Vue({
    el: '#wrapper',
    data: function() {
        return {
            dialogFormVisible: false,
            form: {
            	username: ''
			},
            rules:{
                username: [
                    { required: true, message: '请输入会员名称', trigger: 'blur' },
                ]
            },
            otherServerList:[]
		}
    },
    methods: {
        /**
		 * 显示账号切换框.
         */
        changAccount: function () {
			this.dialogFormVisible = true;
        },
        /**
		 * 确认切换.
         */
        makeChange: function () {
            this.$refs['ruleForm'].validate(function(valid){
                if(valid) {
                    var service = {};
                    service.username = vmGaea.form.username;
                    service.direct = 1;
                    var fn="changeUser";
                    service = Commonjs.jsonToString(service)
                    var params = Commonjs.getParams(fn,service);//获取参数
                    Commonjs.ajaxSilence(weburl,params,true, function (data) {
                        location.href = data.data.template + 'usercenter/index.html';
                    }, function (data) {
						vmGaea.$message.error(data.msg);
                    });
                }
            });
        },
        getOtherProCat:function(){
        	var self = this;
        	var fn="usableDiyProCat";
        	var params = Commonjs.getParams(fn,"");//获取参数
			var data=Commonjs.ajax(weburl,params,false);
			var ss = data;
    //     	var service = {};
    //     	var fn="usableDiyProCat";
    //     	var params = Commonjs.getParams(fn,service);//获取参数
    //         Commonjs.ajaxSilence(weburl,params,true, function (data) {
    //         	self.otherServerList = data.data;
    //         }, function (data) {
				// vmGaea.$message.error(data.msg);
    //         });
        }
    },
    mounted: function () {
    	// this.getOtherProCat();
    }
});
